package com.flyee.viewer
{
	import com.flyee.utils.GenerateIDTool;
	import com.flyee.utils.PlatformDepot;
	
	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	public class TargetWindowViewer extends Sprite implements Destroy
	{
		public var vid:String = GenerateIDTool.generateID();
		/**
		* 目标窗口的背景
		*/		
		private var targetBG:Bitmap;
		/**
		 * 显示目标的名称标签
		 */		
		private var _nameLabel:TextField = null;
		private var nameContainer:Sprite = null;
		private var hpShape:Shape = null;
		private var hpLabel:TextField = null;
		
		private var mpLabel:TextField = null;
		private var mpShape:Shape = null;
		/**
		 * 目标窗口的名称 
		 */		
		private var _targetName:String = "test";
		private const HP_TYPE:int = 1;
		private const MP_TYPE:int = 2;
		
		public function TargetWindowViewer()
		{
			super();
			targetBG = new Bitmap(PlatformDepot.getInstance().getBitmapDataByName("TargetBitmapData"));
			this.addChild(targetBG);
			
			_nameLabel = createLabel(16);
			_nameLabel.text = _targetName;
			
			hpLabel = createLabel(12);
			hpLabel.x = 59;
			hpLabel.y = 2;
			mpLabel = createLabel(12);
			mpLabel.x = 59;
			mpLabel.y = 18;
			
			nameContainer = new Sprite();
			nameContainer.x = 21;
			nameContainer.y = 33;
			nameContainer.graphics.clear();
			nameContainer.graphics.beginFill(0x000000,0);
			nameContainer.graphics.drawRect(0,0,125,20);
			nameContainer.graphics.endFill();
			_nameLabel.x = (125 - _nameLabel.width)/2;
			nameContainer.addChild(_nameLabel);
			
			this.addChild(nameContainer);
			
			hpShape = new Shape();
			mpShape = new Shape();
			
			hpShape.x = 5;
			hpShape.y = 5;
			
			mpShape.x = 5;
			mpShape.y = 21;
			
			drawProgress(hpShape.graphics,155,HP_TYPE);
			drawProgress(mpShape.graphics,155,MP_TYPE);
			
			this.addChild(hpShape);
			this.addChild(mpShape);
			this.addChild(hpLabel);
			this.addChild(mpLabel);
			this.width = targetBG.width;
			this.height = targetBG.height;
			
			hp = 500;
			mp = 500;
		}
		
		public function set targetName(n : String) : void {
			_targetName = n;
			_nameLabel.text = n;
			_nameLabel.x = (nameContainer.width - _nameLabel.textWidth)/2;
		}
		
		public function set hp(hp : int) : void {
			if(hp > 500)
				hp = 500;
			var currentWidth : int = (hp) / 500 * 155;
			hpLabel.text = hp+"/500";
			drawProgress(hpShape.graphics, currentWidth, HP_TYPE);
		}
		
		public function set mp(mp : int) : void {
			if(mp > 500)
				mp = 500;
			var currentWidth : int = (mp) / 500 * 155;
			mpLabel.text = mp+"/500";
			drawProgress(mpShape.graphics, currentWidth, MP_TYPE);
		}
		
		private function drawProgress(graph:Graphics,width:int,type:int):void{
			var colors:Array = null;
			var alphs:Array = [1,1];
			var ratios:Array = [55, 255]; 
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(width,10,Math.PI/2,0,0);
			if(type == HP_TYPE){
				colors = [0xff6944,0xb90000];
			}else{
				colors = [0x02adff,0x006596];
			}
			graph.clear();
			graph.beginGradientFill(GradientType.LINEAR,colors,alphs,ratios,matrix);
			graph.drawRoundRectComplex(0,0,width,10,3,3,3,3);
			graph.endFill();
		}
		
		private function getDropFilter():DropShadowFilter{
			var distance:Number = 1;
			var angle:Number = 90;
			var color:Number = 0x577485;
			var alpha:Number = 0.65;
			return new DropShadowFilter(distance,angle,color,alpha,0,0,1);
		}
		
		private function createLabel(size:int):TextField{
			var lab:TextField = new TextField();
			var format:TextFormat = new TextFormat();
			format.font = "宋体";
			format.size = size;
			format.color = 0xffffff;
			lab.selectable = false;
			lab.autoSize = TextFieldAutoSize.LEFT;
			lab.type = TextFieldType.DYNAMIC;
			lab.defaultTextFormat = format;
			var filter:DropShadowFilter = getDropFilter();
			lab.filters = [filter];
			return lab;
		}
		
		public function dispose():void
		{
		}
		
	}
}